package com.yoho.vpm.custom

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.yoho.vpm.R


class BottomBarView : RelativeLayout {

    constructor(ctx: Context) : this(ctx, null)
    constructor(ctx: Context, attrs: AttributeSet?) : this(ctx, attrs, 0)
    constructor(ctx: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(ctx, attrs, defStyleAttr) {
        initView(context, attrs, defStyleAttr)
    }

    private var msgCount: Int = 0
    private val bar_num: TextView
    private val bar_tv: TextView

    private var selectedSrc: Int=0

    private val bar_iv: ImageView


    init {
        val rl = LayoutInflater.from(context).inflate(R.layout.simple_layout, this, true) as RelativeLayout
        bar_num = rl.findViewById(R.id.bar_num)
        bar_iv = rl.findViewById(R.id.bar_iv)
        bar_tv = rl.findViewById(R.id.bar_tv)

    }

    private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomBarView, defStyleAttr, 0)
        val imgHeight = typedArray.getDimension(R.styleable.BottomBarView_imgHeight, SizeUtils.dp2px(30f).toFloat())
        val imgWidth = typedArray.getDimension(R.styleable.BottomBarView_imgWidth, SizeUtils.dp2px(30f).toFloat())
        val imgSrc = typedArray.getResourceId(R.styleable.BottomBarView_imgSrc, 0)
        selectedSrc = typedArray.getResourceId(R.styleable.BottomBarView_selectedSrc, 0)
        val text = typedArray.getString(R.styleable.BottomBarView_text)
        val textSize = typedArray.getDimension(R.styleable.BottomBarView_textSize, 12f)
        val textColor = typedArray.getColor(R.styleable.BottomBarView_textColor, Color.BLACK)
        val numBg = typedArray.getResourceId(R.styleable.BottomBarView_numBg, 0)
        val color = typedArray.getColor(R.styleable.BottomBarView_numColor, Color.BLUE)
        val numShow = typedArray.getBoolean(R.styleable.BottomBarView_numShow, false)
        typedArray.recycle()
        init(imgSrc, imgHeight, imgWidth, text, textSize, textColor, numShow, color, numBg)

    }

    private fun init(imgSrc: Int, imgHeight: Float, imgWidth: Float, text: String?, textSize: Float, textColor: Int, numShow: Boolean, color: Int, numBg: Int) {
        if (imgSrc != 0) {
            bar_iv.setImageResource(imgSrc)
        }
        val params = bar_iv.layoutParams
        params.height = imgHeight.toInt()
        params.width = imgWidth.toInt()
        bar_iv.layoutParams = params
        bar_tv.text = text
        bar_tv.textSize = textSize
        Log.e("dimension  ", textSize.toString() + "")
        bar_tv.setTextColor(textColor)
        if (numShow) {
            bar_num.visibility = View.VISIBLE
        } else {
            bar_num.visibility = View.GONE
        }
        bar_num.setTextColor(color)
        bar_num.setBackgroundResource(numBg)
    }

    private fun setMessageCount(count: Int) {
        msgCount = count
        if (count == 0) {
            bar_num.visibility = View.GONE
        } else {
            bar_num.visibility = View.VISIBLE
            if (count < 100) {
                bar_num.text = count.toString() + ""
            } else {
                bar_num.text = "99+"
            }
        }
        invalidate()
    }

    fun addMsg() = setMessageCount(msgCount + 1)

    override fun setClickable(clickable: Boolean) {
        super.setClickable(clickable)
        if(!clickable&&selectedSrc!=0) {
            bar_iv.setImageResource(selectedSrc)
        }
    }

}

